package com.woniu.crowd.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.woniu.crowd.client.SelectManageClient;
import com.woniu.crowd.client.param.*;
import com.woniu.crowd.common.util.OssUtil;
import com.woniu.crowd.infra.model.SpuAttachment;
import com.woniu.crowd.service.*;
import com.woniu.crowd.service.form.*;
import com.woniu.product.client.ProductManageClient;
import com.woniu.product.client.param.AddProductParam;
import com.woniu.result.Result;
import com.woniu.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@Slf4j
@RestController
public class SelectManageApiImpl implements SelectManageClient {
    @Resource
    private ProductManageClient productManageClient;
    @Resource
    private SelectProductService selectProductService;
    @Resource
    private SelectApplyService selectApplyService;
    @Resource
    private SpuAttachmentService spuAttachmentService;
    @Resource
    private ShopTestService shopTestService;
    @Resource
    private UserOpinionService userOpinionService;
    @Override
    public Result addCrowdProduct(AddSelectProductParam addSelectProductParam) {
        log.info("新增甄选商品:{}", addSelectProductParam);
        AddProductParam addProductParam = BeanUtil.copyProperties(addSelectProductParam, AddProductParam.class);
        addProductParam.setSpuType("SELECT");
        return productManageClient.addProduct(addProductParam);
    }

    @Override
    public Result userApplySelect(AddSelectApplyParam addSelectApplyParam) {
        log.info("用户申请甄选商品:{}", addSelectApplyParam);
        AddSelectApplyForm addSelectApplyForm = BeanUtil.copyProperties(addSelectApplyParam, AddSelectApplyForm.class);
        selectApplyService.userApplySelect(addSelectApplyForm);
        return Result.getInstance().setCode(200).setMsg("申请成功");
    }

    @Override
    public Result upSelect(UpSelectParam upSelectParam) {
        log.info("上架甄选商品：{}",upSelectParam);
        UpSelectForm upSelectForm = BeanUtil.copyProperties(upSelectParam, UpSelectForm.class);
        return selectProductService.upSelect(upSelectForm);
    }

    @Override
    public Result SelectuploadFile(MultipartFile file) throws IOException {
        log.info("查看文件：{}", file);
        String filename = file.getOriginalFilename();
        String type = filename.substring(filename.lastIndexOf("."));
        String name = "lxlg" + System.currentTimeMillis() + type;
        String path = OssUtil.uploadFile("jianzhitailang", name, file.getInputStream());
        SpuAttachment spuAttachment = new SpuAttachment();
        spuAttachment.setOldfileName(filename);
        spuAttachment.setFilePath(path);
        spuAttachment.setFileSuffix(type);
        spuAttachment.setCreateDate(DateUtil.parseDateLong());
        spuAttachment.setFileName(name);
        spuAttachment.setSize(file.getSize());
        spuAttachmentService.save(spuAttachment);
        return Result.getInstance(String.class).setData(path).setCode(200).setMsg("上传成功");
    }

    @Override
    public Result addShopTest(AddShopTestpParam addShopTestpParam) {
        log.info("平台上传内部测试：{}",addShopTestpParam);
        AddShopTestpForm addShopTestpForm = BeanUtil.copyProperties(addShopTestpParam, AddShopTestpForm.class);
        shopTestService.addShopTest(addShopTestpForm);
        return Result.getInstance().setMsg("上传成功").setCode(200);
    }

    @Override
    public Result userAddOpinion(AddUserOpinionParam userOpinionParam) {
        log.info("用户甄选商品建议上传：{}",userOpinionParam);
        AddUserOpinionForm addUserOpinionForm = BeanUtil.copyProperties(userOpinionParam, AddUserOpinionForm.class);
        userOpinionService.AdduserAddOpinion(addUserOpinionForm);
        return Result.getInstance().setCode(200).setMsg("用户建议上传成功");
    }

    @Override
    public Result checkApply(CheckApplyParam checkApplyParam) {
        log.info("审核申请：{}",checkApplyParam);
        CheckApplyForm checkApplyForm = BeanUtil.copyProperties(checkApplyParam, CheckApplyForm.class);
        return  selectApplyService.checkApply(checkApplyForm);
    }


}

